<template>
	<view class="avatarCont">
		<image :src="avatar" mode="widthFix" :style="{'width':avatarWidth+'px'}"></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: '', //头像路径
				avatarWidth: 260, //头像默认宽度
			}
		},
		onNavigationBarButtonTap() {
			this.changeImage();
		},
		onLoad(e) {
			this.avatar = e.url;
			this.getImageInfo();
		},
		methods: {
			// 获取图片信息
			getImageInfo() {
				var _this = this;
				uni.getImageInfo({
					src: _this.avatar,
					success: function(image) {
						_this.avatarWidth = image.width;
					}
				});
			},
			// 选择图片
			changeImage() {
				var _this = this;
				uni.chooseImage({
					count: 1, //默认9
					success: function(res) {
						_this.$zapi.apifile('user/currency.basic/UploadUserImg', { //上传图片
							file: res.tempFilePaths[0]
						}).then(res => {
							var result = JSON.parse(res.data);
							if (result.code == 200) {
								_this.avatar = result.data.visit_path;
								_this.$zapi.apiPost('user/currency.basic/UpdateBasic', {avatar: _this.avatar}).then(res => { //上传图片成功 & 修改个人信息
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
									if (res.data.code == 200) {
										_this.$zapi.apiPost('user/currency.basic/GetUserInfo').then(res => { //修改个人信息成功 & 获取个人信息并替换缓存
											uni.setStorageSync('userInfo', res.data.data);
											_this.getImageInfo();
										})
									}
								})
							} else {
								uni.showToast({
									title: '上传失败,请重新上传',
									icon: 'none'
								})
							}
						})
					}
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.avatarCont {
		display: block;
		position: fixed;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 999;
		background: rgba(0, 0, 0, .8);
		display: flex;
		justify-content: center;
		align-items: center;
		/* #ifdef APP-PLUS */
		top: 0;
		/* #endif */
		/* #ifdef H5 */
		top: 88upx;
		/* #endif */
	}

	image {
		position: absolute;
		max-height: 100%;
		max-width: 100%;
	}
</style>
